//
//  HWDiscountDaiChunViewController.m
//  HWPromoteProjectAPP
//
//  Created by 陈道斌 on 2020/2/26.
//  Copyright © 2020 陈道斌. All rights reserved.
//

#import "HWDiscountDaiChunViewController.h"
#import "HWGenerationOfFillingViewController.h"  //代充记录
#import "HWGameIcoPopView.h"
#import "HWdaichongzheKouModel.h"

#import "HWChongzhishibaiViewController.h"
#import "HWWebViewGameViewController.h"
#import "HWApplyForSettlementViewController.h" // 成功






@interface HWDiscountDaiChunViewController ()<HWGameIcoPopViewDlegate,UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIButton *zhifubaoBut;
@property (weak, nonatomic) IBOutlet UIButton *yufukuanBtn;


@property (weak, nonatomic) IBOutlet UITextField *shiwanzhanghaoTxetField;

@property (weak, nonatomic) IBOutlet UIImageView *icoiamgeView; // 游戏图片
@property (weak, nonatomic) IBOutlet UILabel *youmingchengLabl;
@property (weak, nonatomic) IBOutlet UITextField *shurujingETextField;
@property (weak, nonatomic) IBOutlet UILabel *zhekeouLabel; //折扣金额
@property (weak, nonatomic) IBOutlet UILabel *zhifiLabel; // 支付金额

@property(strong,nonatomic) NSString *jiLuStr; // 1代表 支付  2 代表 预付款
@property(strong,nonatomic) HWdaichongzheKouModel *selectedModel; // 选中的游戏

@property (strong,nonatomic) NSString *oidStr;


@end

@implementation HWDiscountDaiChunViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    // 设置UI
    [self setupUI];
    
    
    [self setupNaVigationView];
    
    [self shezhichangyongshuxing];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(shouqingjianpan:)];
    [self.view addGestureRecognizer:tap];
    
    [self yufukuanjinger];
    
        
}




- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    if (self.oidStr != nil) {
        
       [self chaxundingdang];
        
    }
    
}



//  查询订单
- (void)chaxundingdang {
    
    
    
    
    

    NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
    NSString *uid =  [HWLocallyStoredData getLoginUserID];
    NSString *signStr = [NSString stringWithFormat:@"%@%@%@%@",uid,self.oidStr,tStr,@"adesxloksy13s98adv"];
    NSString  *signStrMd5  =   [WMUtil md5HexDigest:signStr];

    NSDictionary *parametes = @{        @"ios" : @"1",
                                   @"ct" : @"app",
                                   @"ac" : @"fuli_status",
                                   @"t" : tStr,
                                   @"oid" : self.oidStr,
                                   @"uid":uid,
                                   @"sign" :signStrMd5
                                   };


       [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {



             NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
             if ([codeStr intValue] == 1) {

                      NSLog(@"%@",response);
                 
                HWApplyForSettlementViewController *shiba = [[HWApplyForSettlementViewController alloc]init];
                [self addChildViewController:shiba];
                shiba.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
                [self.view addSubview:shiba.view];


                    

             } else {


                 
                    HWChongzhishibaiViewController *shiba = [[HWChongzhishibaiViewController alloc]init];
                   [self addChildViewController:shiba];
                   shiba.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
                   [self.view addSubview:shiba.view];
                 
                 


             }



         } failure:^(NSString *error) {
            // [SVProgressHUD dismiss];

             [MBProgressHUD showSuccess:@"登录失败"];
             [MBProgressHUD hideHUD];




         }];

    
    
    
    
    
//    HWChongzhijiemianViewController  *chong = [[HWChongzhijiemianViewController alloc]init];
//    chong.oidStr = self.oidStr;
//    chong.chongzhijinger = self.chongzhijinger;
//    [self.navigationController pushViewController:chong animated:YES];
    
    
    
    
    
    
    
    
    
    
}







// 预付款余额 预付费余额
- (void)yufukuanjinger {
    
    
     NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
     NSString *uid =  [HWLocallyStoredData getLoginUserID];
     NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tStr,@"adesxloksy13s98adv"];
     NSString  *signStrMd5  =   [WMUtil md5HexDigest:signStr];
       
    
    
    
      NSDictionary *parametes = @{      @"ios" : @"1",
                                   @"ct" : @"app",
                                    @"ac" : @"yufu",
                                    @"t" : tStr,
                                    @"uid":uid,
                                    @"sign" :signStrMd5
                                    };
    
    
        [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
              
             

              NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
              if ([codeStr intValue] == 1) {
             
                    NSLog(@"%@",response);
                  
                  NSString *jingerstr = [NSString stringWithFormat:@"预付款(%@)",response[@"data"]];
                  [self.yufukuanBtn  setTitle:jingerstr forState:UIControlStateNormal];
                  
                  
            
            

                   
              } else {
                  
                [MBProgressHUD showError:response[@"message"]];
                                
            
                  
              }
              
         
              
          } failure:^(NSString *error) {
             // [SVProgressHUD dismiss];
              
              [MBProgressHUD showSuccess:@"登录失败"];
              [MBProgressHUD hideHUD];
              
              
              
              
          }];
    
    
    
    
}





- (void)shouqingjianpan:(UITapGestureRecognizer *)sender {
    
    [self.view endEditing:YES];
    
    
    NSString *shuruJinge = [NSString stringWithFormat:@"%@",self.shurujingETextField.text];
    
    CGFloat jinger = [shuruJinge floatValue];
    
    
    if ([self.shurujingETextField.text isEqualToString:@""]) {
        
        
    } else if (jinger > 0) {
        
        if (self.selectedModel == nil) {
            return;
        }
        
        
        HWdaichongzheKouModel *model =    self.selectedModel;
        
        
        CGFloat zuizhogzekou =    1 - model.zhekou;
        
        CGFloat zuizhongjinger = jinger*zuizhogzekou;
    
      //   支付金额 ￥0
        
        self.zhifiLabel.text  = [NSString stringWithFormat:@"支付金额￥%.2f",zuizhongjinger];
        
        
        
        
        
        
        
    }
    
    
    
    
    
    
    
    
    
}






- (void)setupNaVigationView  {
    
    
      UIBarButtonItem *buttonItem =  [[UIBarButtonItem alloc]initWithTitle:@"代充记录" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonTap)];
      buttonItem.tintColor  = RGB(68, 68, 68);
      self.navigationItem.rightBarButtonItem = buttonItem;
    
}

// 设置UI
-(void)setupUI {
     
    self.view.backgroundColor = [UIColor redColor];
    self.view.backgroundColor = RGB(245, 245, 245);
    self.navigationItem.title = @"折扣代充";

    self.jiLuStr = @"1";
    
    
    
}


// 设置常用的属性
- (void)shezhichangyongshuxing {
    
    self.icoiamgeView.layer.cornerRadius = 8;
    self.icoiamgeView.layer.masksToBounds = YES;
    self.icoiamgeView.backgroundColor = [UIColor whiteColor];
    
    self.youmingchengLabl.text = @"";
    
    self.shurujingETextField.keyboardType = UIKeyboardTypeDecimalPad;
    
    self.shurujingETextField.delegate = self;
    
    

}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    if (self.selectedModel == nil) {
        
        [MBProgressHUD showError:@"请先选择游戏"];
        return NO;
        
        
        
    } else {
        
        
        return  YES;
    }
    
    
    
}


//
- (void)rightBarButtonTap {
    
    
    HWGenerationOfFillingViewController *GenerationOf = [[HWGenerationOfFillingViewController alloc]init];
    [self.navigationController pushViewController:GenerationOf animated:YES];

    
    
}

#pragma mark ------- HWGameIcoPopViewDelegate

- (void)HWGameIcoPopViewDelegate:(HWdaichongzheKouModel *)model {
    
   
//    if ([self.shiwanzhanghaoTxetField.text isEqualToString:@""]) {
//
//         [MBProgressHUD showError:@"请输入游戏账号"];
//          return;
//
//    }
    
    
    self.selectedModel = model;

    self.shurujingETextField.text = @"";
    self.zhifiLabel.text = [NSString stringWithFormat:@"%@",@"支付金额 ￥0"];
    
    
    
    [self.icoiamgeView  sd_setImageWithURL:[NSURL URLWithString:model.icon] placeholderImage:nil];
    self.youmingchengLabl.text =  [NSString stringWithFormat:@"%@",model.name];
    self.zhekeouLabel.text =  [NSString stringWithFormat:@"折扣：%d%%",(int)(model.zhekou*100)];
    NSLog(@"zhe %d",(int)(model.zhekou*100));
}



//选择支付
- (IBAction)xuanzeZhifuBut:(UIButton *)sender {
    

    
    
    NSLog(@"选择支付");
    self.zhifubaoBut.backgroundColor = [UIColor whiteColor];
    self.yufukuanBtn.backgroundColor = beijingyanSe;
     self.jiLuStr = @"1";

    
}


// 预付款
- (IBAction)yufukuanBut:(UIButton *)sender {
    
     NSLog(@"预付款");
    self.zhifubaoBut.backgroundColor =  beijingyanSe;
    self.yufukuanBtn.backgroundColor = [UIColor whiteColor];
    self.jiLuStr = @"2";
    
}

//  请选择金额

- (IBAction)xuanZeBut:(UIButton *)sender {
    
      NSLog(@"请选择游戏");
   

      if ([self.shiwanzhanghaoTxetField.text isEqualToString:@""]) {
          
          [MBProgressHUD showError:@"请输入游戏账号"];
                return;
    
      }
    [self.view endEditing:YES];
    
      HWGameIcoPopView  *fenxiang = [[HWGameIcoPopView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
      fenxiang.delegate = self;
      [fenxiang requestDatashuju];
      [kKeyWindow addSubview:fenxiang];
    
    
    
    

    
}

// 提交BUtton
- (IBAction)tijiaobutonti:(UIButton *)sender {
    
    
     NSLog(@"提交BUtton");
    
    
    
      NSString *shuruJinge = [NSString stringWithFormat:@"%@",self.shurujingETextField.text];
        
      CGFloat jinger = [shuruJinge floatValue];
    
      if ([self.shiwanzhanghaoTxetField.text isEqualToString:@""]) {
          
          [MBProgressHUD showError:@"请输入游戏账号"];
                return;
    
      }  else if (self.selectedModel == nil) {
          
          [MBProgressHUD showError:@"请选择游戏"];
            return;
          
      } else if ([shuruJinge isEqualToString:@""]) {
          
          
          [MBProgressHUD showError:@"请输入金额"];
          
           return;
          
      } else if (jinger == 0) {
      
          [MBProgressHUD showError:@"请输入正确的金额"];
                   
           return;
    
      }
    
    
    
    
    if ([self.jiLuStr isEqualToString:@"1"]) {
        
        
        [self payTreasurezhifu];
        
    } else if ([self.jiLuStr isEqualToString:@"2"]) {
        
        [self yufukuanappdhjiekou];
        
    }
    
    
    
    
    
}


// 支付宝支付

- (void)payTreasurezhifu   {
    
    
    
   // md5(uid+username+money+gid+type+t+adesxloksy13s98adv
      HWdaichongzheKouModel *mdle =  self.selectedModel;
     NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
     NSString *uid =  [HWLocallyStoredData getLoginUserID];
     NSString *userName =    [NSString stringWithFormat:@"%@",self.shiwanzhanghaoTxetField.text];
     NSString *shuruJinge = [NSString stringWithFormat:@"%@",self.shurujingETextField.text];
     NSString *type = [NSString stringWithFormat:@"0"];
     NSString *signStr = [NSString stringWithFormat:@"%@%@%@%@%@%@%@",uid,userName,shuruJinge,mdle.id,type,tStr,@"adesxloksy13s98adv"];
     NSString  *signStrMd5  =   [WMUtil md5HexDigest:signStr];
       
    //  NSString *shuruJinge = [NSString stringWithFormat:@"%@",self.shurujingETextField.text];
    
    //  HWdaichongzheKouModel *mdle =  self.selectedModel;
    
   
    
    
      NSDictionary *parametes = @{      @"ios" : @"1",
                                   @"ct" : @"app",
                                    @"ac" : @"fuli",
                                    @"money": shuruJinge,
                                    @"gid" : mdle.id,
                                    @"type" : @"0",
                                    @"username" : userName,
                                    @"t" : tStr,
                                    @"uid":uid,
                                    @"sign" :signStrMd5
                                    };
    
    
        [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
              
             

              NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
              if ([codeStr intValue] == 1) {
             
                    NSLog(@"%@",response);
                  self.oidStr = [NSString stringWithFormat:@"%@",response[@"data"][@"oid"]];
                  
                   HWWebViewGameViewController *webview = [[HWWebViewGameViewController alloc]init];
                    webview.urlString = [NSString stringWithFormat:@"%@",response[@"data"][@"url"]];
                    webview.gname = @"充值";
                    webview.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:webview animated:YES];
                  
                  
                  //  [self.navigationController popViewControllerAnimated:YES];
                  
                    
            

                   
              } else {
                  
                [MBProgressHUD showError:response[@"message"]];
                                
            
                  
              }
              
         
              
          } failure:^(NSString *error) {
             // [SVProgressHUD dismiss];
              
              [MBProgressHUD showSuccess:@"登录失败"];
              [MBProgressHUD hideHUD];
              
              
              
              
          }];
    
    
    
    
    
    
    
}


// 预付款

- (void)yufukuanappdhjiekou {
    
    
    
    
    HWdaichongzheKouModel *mdle =  self.selectedModel;
      NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
      NSString *uid =  [HWLocallyStoredData getLoginUserID];
      NSString *userName =    [NSString stringWithFormat:@"%@",self.shiwanzhanghaoTxetField.text];
      NSString *shuruJinge = [NSString stringWithFormat:@"%@",self.shurujingETextField.text];
      NSString *type = [NSString stringWithFormat:@"1"];
      NSString *signStr = [NSString stringWithFormat:@"%@%@%@%@%@%@%@",uid,userName,shuruJinge,mdle.id,type,tStr,@"adesxloksy13s98adv"];
      NSString  *signStrMd5  =   [WMUtil md5HexDigest:signStr];
        
     //  NSString *shuruJinge = [NSString stringWithFormat:@"%@",self.shurujingETextField.text];
     
     //  HWdaichongzheKouModel *mdle =  self.selectedModel;
     
    
     
     
       NSDictionary *parametes = @{      @"ios" : @"1",
                                    @"ct" : @"app",
                                     @"ac" : @"fuli",
                                     @"money": shuruJinge,
                                     @"gid" : mdle.id,
                                     @"type" :type,
                                     @"username" : userName,
                                     @"t" : tStr,
                                     @"uid":uid,
                                     @"sign" :signStrMd5
                                     };
     
     
         [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
               
              

               NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
               if ([codeStr intValue] == 1) {
              
                     NSLog(@"%@",response);
                   

                   [MBProgressHUD showError:response[@"message"]];
                     [self.navigationController popViewControllerAnimated:YES];
                   
             
             

                    
               } else {
                   
                 [MBProgressHUD showError:response[@"message"]];
                                 
             
                   
               }
               
          
               
           } failure:^(NSString *error) {
              // [SVProgressHUD dismiss];
               
               [MBProgressHUD showSuccess:@"登录失败"];
               [MBProgressHUD hideHUD];
               
               
               
               
           }];
    
    
    
    
}






@end
